package com.dbh.company.buildbook.gif;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.img.gif.AnimatedGifEncoder;
import java.awt.image.BufferedImage;


/**
 * @author dongbanghui
 * @create 2024/1/12
 */
public class GifPlay {

    public static void main(String[] args) {
        // 创建StopWatch对象
        StopWatch watch = new StopWatch();
        // 开始计时
        watch.start();
        try {


            // 创建AnimatedGifEncoder
            AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();
            gifEncoder.start("D:\\data\\work\\file\\楼书\\楼书1.0\\gif图\\result\\1.gif");
            gifEncoder.setRepeat(0);  // 设置为循环播放
            gifEncoder.setDelay(50);  // 设置每帧之间的延迟，单位是毫秒

            // 读取图片文件并添加到GIF
            for (int i = 0; i < 90; i++) {
                String imagePath = "D:\\data\\work\\file\\楼书\\楼书1.0\\gif图\\noise\\" + i + ".jpg";
                BufferedImage image = ImgUtil.read(imagePath);
                gifEncoder.addFrame(image);
            }

            // 结束GIF
            gifEncoder.finish();

            // 停止计时
            watch.stop();

            // 输出耗时
            System.out.println("方法执行耗时：" + watch.getTotalTimeMillis() + "毫秒");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
